package com.jia.leetCode;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: Leetcode
 * @description:  https://leetcode.com/problems/subarray-sum-equals-k/
 * @author: STU756
 * @create: 2020-08-22 13:15
 */
public class Pro0560 {
    public int subarraySum(int[] nums, int k) {
        //前缀和
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, 1);
        int sum = 0;
        int ans = 0;
        for(int i = 0; i < nums.length; i++) {
            sum += nums[i];
            if(map.containsKey(sum - k)) {
                ans += map.get(sum - k);
            }
            map.put(sum, map.getOrDefault(sum, 0) + 1);
        }
        return  ans;
    }
}
